package com.dashan.backend.datastore;

import static com.dashan.Constants.COORD_FORMAT;

import java.util.Date;

import javax.jdo.annotations.Embedded;
import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.IdentityType;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;

import org.w3c.dom.Document;
import org.w3c.dom.Element;

import com.dashan.Constants;
import com.dashan.backend.XmlSerializable;
import com.google.appengine.api.datastore.Key;

@PersistenceCapable(identityType = IdentityType.APPLICATION)
public class DsPosition implements Cloneable, XmlSerializable {
    @PrimaryKey
    @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
    private Key key;

    @Persistent
    private String userName;
    
    @Persistent
    private String description = "";

    @Persistent
    private Date timestamp;
    
    @Persistent
    private double longitude;
    
    @Persistent
    private double latitude;
    
    @Persistent
    private double accuracy;
    
    @Persistent
    private boolean current;
    
    @Persistent 
    @Embedded
    private DsGeoCellArray dsGeoCellArray;
    
    public DsPosition(String userName, double lon, double lat,
            double accuracy, Date timestamp) {
        this.userName = userName;
        this.longitude = lon;
        this.latitude = lat;
        this.accuracy = accuracy;
        this.timestamp = timestamp;
        this.current = true;
        this.dsGeoCellArray = new DsGeoCellArray(lon, lat);
    }
    
    public String getUserName() {
        return userName;
    }
    
    public Date getTimestamp() {
        return timestamp;
    }

    public double getLongitude() {
        return longitude;
    }

    public double getLatitude() {
        return latitude;
    }

    public double getAccuracy() {
        return accuracy;
    }

    public Key getKey() {
        return key;
    }
    
    public void setCurrent(boolean current) {
        this.current = current;
    }
    
    public boolean isCurrent() {
        return current;
    }

    public DsGeoCellArray getDsGeoCellArray() {
        return dsGeoCellArray;
    }
    
    /* (non-Javadoc)
     * @see com.dashan.backend.datastore.XmlDescriptable#getXmlElement(org.w3c.dom.Document)
     */
    public Element getXmlElement(Document xmlDoc) {
        Element root = xmlDoc.createElement("position");
        if (getTimestamp() != null) {
            Element ele = null;    
            ele = xmlDoc.createElement("lng_lat_acc");
            ele.appendChild(xmlDoc.createTextNode(
                    String.format(COORD_FORMAT + ", " + COORD_FORMAT + ", " + COORD_FORMAT,
                                  getLongitude(), getLatitude(),  getAccuracy())));
            root.appendChild(ele);
            ele = xmlDoc.createElement("timestamp");
            ele.appendChild(xmlDoc.createTextNode(getTimestamp().toString()));
            root.appendChild(ele);
            ele = xmlDoc.createElement("timestamp_long");
            ele.appendChild(xmlDoc.createTextNode(getTimestamp().getTime() + ""));
            root.appendChild(ele);
            if (getDescription().length() != 0) {
                ele = xmlDoc.createElement(Constants.POSITION_DESC);
                ele.appendChild(xmlDoc.createTextNode(getDescription()));
                root.appendChild(ele);
            }
        }
        return root;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public String getDescription() {
        if (description != null) {
            return description;
        } else {
            return "";
        }
    }
}
